package com.zxb.base.autoform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxb.core.common.A;
import com.zxb.core.common.C;
import com.zxb.core.common.R;
import com.zxb.core.dao.autoform.DiyForm;
import com.zxb.core.dao.autoform.DiyFormDao;
import com.zxb.core.dao.autoform.DiyFormula;
import com.zxb.core.dao.autoform.DiyFormulaDao;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
public class FormulaController {
    private final DiyFormulaDao formulaDao;
    private final DiyFormDao formDao;

    @PostMapping("/formula/add")
    public R add(@RequestBody DiyFormula obj) {
        if (formulaDao.exists(
                new QueryWrapper<DiyFormula>()
                        .lambda()
                        .eq(DiyFormula::getFormId, obj.getFormId())
                        .eq(DiyFormula::getName, obj.getName())
        )) {
            return R.error("公式名称已经存在");
        }
        obj.setSearchStr(obj.getName() + obj.getCata() + obj.getTriggerMode());
        formulaDao.save(obj);
        //统计公式数量
        Long formulaCount = formulaDao.count(new QueryWrapper<DiyFormula>()
                .lambda().eq(DiyFormula::getFormId,obj.getFormId()));
        formDao.lambdaUpdate()
                .eq(DiyForm::getId, obj.getFormId())
                .set(DiyForm::getFormulaCount, formulaCount.intValue())
                .update();
        return R.success();
    }

    @PostMapping("/formula/update")
    public R update(@RequestBody DiyFormula obj) {
        DiyFormula old = formulaDao.getById(obj.getId());
        A.isInDatabase(old, "公式");
        if (formulaDao.exists(
                new QueryWrapper<DiyFormula>()
                        .lambda()
                        .eq(DiyFormula::getFormId, old.getFormId())
                        .eq(DiyFormula::getName, obj.getName())
                        .ne(DiyFormula::getId, old.getId())
        )) {
            return R.error("公式名称已经存在");
        }
        BeanUtil.copyProperties(obj, old, C.IGNORED_PROPERTIES);
        old.setSearchStr(obj.getName() + obj.getCata() + obj.getTriggerMode());
        formulaDao.updateById(old);
        return R.success();
    }

    @PostMapping("/formula/del")
    public R del(@RequestBody List<Long> ids) {
        if(CollUtil.isNotEmpty(ids)){
            DiyFormula formula = formulaDao.getById(ids.get(0));
            Long formId = formula.getFormId();
            //删除字段
            formulaDao.removeBatchByIds(ids);
            //统计表单字段数量
            Long formulaCount = formulaDao.count(new QueryWrapper<DiyFormula>()
                    .lambda().eq(DiyFormula::getFormId, formId));
            formDao.lambdaUpdate()
                    .eq(DiyForm::getId, formId)
                    .set(DiyForm::getFormulaCount, formulaCount.intValue())
                    .update();
        }
        return R.success();
    }

    @GetMapping("/formula/page")
    public R page(String searchStr, Long formId, Integer page, Integer pageSize) {
        IPage<DiyFormula> pageParam = new Page<>(page, pageSize);
        QueryWrapper<DiyFormula> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(searchStr)) {
            queryWrapper.lambda().like(DiyFormula::getSearchStr, "%" + searchStr + "%");
        }
        if (formId != null) {
            queryWrapper.lambda().eq(DiyFormula::getFormId, formId);
        }
        IPage<DiyFormula> result = formulaDao.page(pageParam, queryWrapper);
        return R.success(result);
    }

    @GetMapping("/formula/count")
    public R countFormula(Long formId) {
        long count = formulaDao.count(new QueryWrapper<DiyFormula>()
                .lambda().eq(DiyFormula::getFormId, formId));
        return R.success(count);
    }
}
